import React, { useState, useEffect, useCallback } from 'react';
import { getSysDepartmentFindList, getSofContractSelected } from '../../../../../../Api/listUrl';
import { Form, Button, Select, Row, Col, message, DatePicker, Input } from "antd";
import moment from 'moment';

const { Option } = Select;
const { RangePicker } = DatePicker;

/**
 * @description: 材料交易单 -- 出库 -- 搜索组件
 * @author: xiaoxujs
 * @updata: xiaoxujs（2021-09-18） 创建组件
 */

const SelectModule = ({ handleSelectModuleOk, handleSelectModuleReset }) => {
    const [form] = Form.useForm()
    const { resetFields } = form;
    const [departmentList, setDepartmentList] = useState([]); //部门list
    const [contractList, setContractList] = useState([]); //合同list
    const contractFun = useCallback(
        () => {
            ; (async () => {
                const { code, msg, data } = await getSofContractSelected();
                if (code === '20000') {
                    setContractList(data)
                } else {
                    message.error(msg);
                }
            })();
        },
        [],
    )
    const departmentFun = useCallback(
        () => {
            ; (async () => {
                const { code, msg, data } = await getSysDepartmentFindList();
                if (code === '20000') {
                    setDepartmentList(data);
                } else {
                    message.error(msg);
                }
            })();
        },
        [],
    )
    useEffect(() => {
        departmentFun();
        contractFun();
    }, [departmentFun, contractFun])
    const handleReset = () => {
        resetFields()
        handleSelectModuleReset()
    }
    const onFinish = (values) => {
        if(values.time) {
            values.outStartBoundTime = moment(values.time[0]).format('YYYY-MM-DD');
            values.outEndBoundTime = moment(values.time[1]).format('YYYY-MM-DD');
            delete values.time;
        }
        console.log(values);
        handleSelectModuleOk(values)
    };
    return <Form
        {...layout}
        name="basic"
        initialValues={{ remember: true }}
        onFinish={onFinish}
        form={form}
    >
        <Row>
            <Col span={6}>
                <Form.Item name="time" label="交货时间" hasFeedback>
                    <RangePicker />
                </Form.Item>
            </Col>
            <Col span={6}>
                <Form.Item name="conNo" label="合同编号" hasFeedback>
                    <Select placeholder="请选择合同编号">
                        {
                            contractList.map(item => <Option value={item.name} key={item.id}> {item.name} </Option>)
                        }
                    </Select>
                </Form.Item>
            </Col>
            <Col span={6}>
                <Form.Item name="product" label="产品名称" hasFeedback>
                    <Input placeholder = '请输入' />
                </Form.Item>
            </Col>
            <Col span={6}>
                <Form.Item name="assumeDep" label="出售部门" hasFeedback>
                    <Select placeholder="请选择领用部门">
                        {
                            departmentList.map(item => <Option value={item.id} key={item.id}> {item.name} </Option>)
                        }
                    </Select>
                </Form.Item>
            </Col>

        </Row>
        <Row>
            <Col span={24} className="bottom_button_box">
                <Form.Item wrapperCol={{ offset: 0, span: 24 }}>
                    <Button 
                        type="primary" 
                        htmlType="submit" 
                        style = {{
                            height: '38px', 
                            width: '78px',
                            borderRadius: '4px'
                        }}
                    >
                        搜索
                    </Button>
                    <Button 
                        onClick={handleReset} 
                        style = {{
                            height: '38px', 
                            width: '78px',
                            borderRadius: '4px'
                        }}
                    >重置</Button>
                </Form.Item>
            </Col>
        </Row>
    </Form>
}
const layout = {
    labelCol: { span: 8 },
    wrapperCol: { span: 14 },
};
export default SelectModule;